package org.easydev.base.webbase.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;


@Configuration
public class JacksonConfig {

    /**
     * Jackson2ObjectMapperBuilderCustomizer 是 Spring Boot 中定制 ObjectMapper 的推荐方式之一，通常比直接 @Bean 一个 ObjectMapper 更好。
     * 它能更好地与 Spring Boot 的自动配置机制集成，避免覆盖可能由其他配置（如 spring.jackson.* 属性）设置的属性。
     * 这是 Spring Boot 官方推荐的扩展或修改默认 ObjectMapper 的方式。
     * @return
     */
    @Bean
    @Primary
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder builder) {
                //SpringBoot 默认会配置 JavaTimeModule
                // 创建 JavaTimeModule
//                JavaTimeModule javaTimeModule = new JavaTimeModule();
                // 将模块添加到 ObjectMapper 构建器中
//                builder.modules(javaTimeModule);
                //禁用一些配置
                builder.featuresToDisable(
                        SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
                        SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS,
                        DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE
                );
            }
        };
    }


}
